package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.Experiment;
import com.siwei.mes.service.ExperimentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账相关
 *
 * @author linzi
 * @since 2023-12-18 22:11:47
 */
@Api(value = "试验台账")
@RestController
@RequestMapping("experiment")
public class ExperimentController {
    @Resource
    private ExperimentService experimentService;

    @ApiOperation(value = "新增试验台账")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) Experiment entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentService.create(entity)));
    }

    @ApiOperation(value = "修改试验台账")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) Experiment entity) {
        experimentService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除试验台账")
    @PostMapping("/delete")
    public Json delete(@RequestBody Experiment entity) {
        experimentService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取试验台账")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Experiment, Experiment> query) {
        return Json.succ().data(experimentService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取试验台账")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentService.loadById(id));
    }

    @ApiOperation(value = "获取所有试验台账")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Experiment entity) {
        return Json.succ().data(ApiResult.addList(experimentService.getAll(entity)));
    }

    @ApiOperation("接收")
    @PostMapping("accept")
    public Json accept(@RequestBody Experiment entity) {
        assertTrue(entity.getId() == null, "试验台账id为空");
        experimentService.accept(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation("完成")
    @PostMapping("finish")
    public Json finish(@RequestBody Experiment entity) {
        assertTrue(entity.getId() == null, "试验台账id为空");
        experimentService.finish(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation("拒绝")
    @PostMapping("refuse")
    public Json refuse(@RequestBody Experiment entity) {
        assertTrue(entity.getId() == null, "试验台账id为空");
        experimentService.refuse(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation("getAllCount")
    @PostMapping("getAllCount")
    public Json getAllCount(@RequestBody Experiment entity) {
        return Json.succ().data(ApiResult.addColumn("totalCount", experimentService.getAllCount(entity)));
    }
}
